package com.cet.programmercarl.algorithmperformancanalysis.二叉树.层级遍历;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @program: algorithm
 * @description: 二叉树的右视图
 * @author: 陈恩涛
 * @create: 2022-07-14 16:58
 **/
public class LC199 {

    public static void main(String[] args) {
        final TreeNode root = new TreeNode(3, new TreeNode(9), new TreeNode(20, new TreeNode(15), new TreeNode(7)));
        System.out.println(new LC199().rightSideView(root));
    }

    public List<Integer> rightSideView(TreeNode root) {
        final List<Integer> result = new ArrayList<>();
        if (root == null) {
            return result;
        }
        final Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            int len = queue.size();
            while (len > 0) {
                final TreeNode temp = queue.poll();
                if (temp.left != null) {
                    queue.offer(temp.left);
                }
                if (temp.right != null) {
                    queue.offer(temp.right);
                }
                if (len == 1) {
                    result.add(temp.val);
                }
                len --;
            }

        }
        return result;
    }
}
